﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Util
{
    public class XMlCRUD
    {

        private static string path = AppDomain.CurrentDomain.BaseDirectory + "resource/Resource.xml";
        /// <summary>
        /// add
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static bool Add(string key, string value)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNode root = doc.DocumentElement;


            XmlNodeList items = root.ChildNodes;
            bool flag = false;
            foreach (XmlNode item in items)
            {
                if (item.Attributes["key"].Value.Equals(key))
                {
                    flag = true;

                }
            }
            if (!flag)
            {
                XmlElement newItem = doc.CreateElement("add");

                //配参
                newItem.SetAttribute("key", key);
                newItem.SetAttribute("value", value);
                //装配，实现其组织结构

                root.AppendChild(newItem);
                //保存xml文档
                doc.Save(path);
                return true;
            }
            return false;
        }

        /// <summary>
        /// update
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static bool Update(string key, string value)
        {
            //提取xml文档
            XmlDocument xd = new XmlDocument();
            xd.Load(path);

            //获取根节点
            XmlNode root = xd.DocumentElement;

            //获取节点类表
            XmlNodeList items = root.ChildNodes;

            //循环节点
            foreach (XmlNode item in items)
            {
                foreach (XmlAttribute attr in item.Attributes)
                {
                    if (attr.Value.Equals(key))
                    {
                        item.Attributes["value"].Value = value;
                        xd.Save(path);
                        return true;
                    }
                }

            }
            return false;

        }

        /// <summary>
        /// delete
        /// </summary>
        /// <param name="key"><key/param>
        public static bool Delete(string key)
        {
            //提取xml文档
            XmlDocument xd = new XmlDocument();
            xd.Load(path);

            //获取根节点
            XmlNode root = xd.DocumentElement;

            //获取节点列表
            XmlNodeList items = root.ChildNodes;

            //循环节点
            foreach (XmlNode item in items)
            {
                foreach (XmlAttribute attr in item.Attributes)
                {
                    if (attr.Value.Equals(key))
                    {
                        root.RemoveChild(item);
                        xd.Save(path);
                        return true;
                    }
                }
            }
            return false;

        }
        /// <summary>
        /// Get all adds
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetAdd()
        {
            //提取xml文档
            XmlDocument xd = new XmlDocument();
            xd.Load(path);

            //获取根节点
            XmlNode root = xd.DocumentElement;

            //获取节点列表
            XmlNodeList items = root.ChildNodes;
            Dictionary<string, string> all = new Dictionary<string, string>();
            //循环节点
            foreach (XmlNode item in items)
            {
                all.Add(item.Attributes["key"].Value, item.Attributes["value"].Value);
            }
            return all;
        }
        public static string GetAdd(string key)
        {
            //提取xml文档
            XmlDocument xd = new XmlDocument();
            xd.Load(path);

            //获取根节点
            XmlNode root = xd.DocumentElement;

            //获取节点列表
            XmlNodeList items = root.ChildNodes;

            //循环节点
            foreach (XmlNode item in items)
            {
                if (item.Attributes["key"].Value.Equals(key))
                {
                    return item.Attributes["value"].Value;
                }
            }
            return null;
        }
    }
}
